package org.supreme.basic.excetion;

import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.supreme.basic.result.JSONResult;

@RestControllerAdvice
public class ExcetionHander {
    @ExceptionHandler(Exception.class) //全局的一个异常错误收集
    public JSONResult extionHander(Exception e){
        e.printStackTrace();
        return JSONResult.error(GlobExcetionEnum.COMMON_NO);
    }
    @ExceptionHandler(GlobalExcetion.class) //自定义的一个异常错误收集
    public JSONResult GlobalExcetion(GlobalExcetion GlobalExcetion){
        GlobalExcetion.printStackTrace();
        return JSONResult.error(GlobalExcetion);
    }
    @ExceptionHandler(MethodArgumentNotValidException.class) //jsr303的一个异常处理
    public JSONResult methodArgumentNotValid(MethodArgumentNotValidException error){
        error.printStackTrace();
        StringBuilder stringBuilder = new StringBuilder();
        error.getBindingResult().getAllErrors().forEach(err-> {
            stringBuilder.append(err.getDefaultMessage());
        });
        return JSONResult.error(GlobExcetionEnum.COMMON_jsr303);
    }

}
